import request from '@/utils/request'

export function fetchList(query) {
  const q = {
    page: query.page,
    limit: query.limit
  }
  if (query.category && query.category !== '') {
    q.category_id = query.category
  }
  if (query.sub_category && query.sub_category !== '') {
    q.category_id = query.sub_category
  }
  if (query.product_name && query.product_name !== '') {
    q.product_name = query.product_name
  }
  if (query.keyword && query.keyword !== '') {
    q.keyword = query.keyword
  }
  return request({
    url: '/api/products',
    method: 'get',
    params: q
  })
}

export function fetchCategories(){
  return request({
    url: '/api/categories',
    method: 'get'
  })
}

export function fetchSoldList(query,user_id) {
  const q = {
    page: query.page,
    limit: query.limit
  }
  if (query.category && query.category !== '') {
    q.category_id = query.category
  }
  if (query.sub_category && query.sub_category !== '') {
    q.category_id = query.sub_category
  }
  if (query.product_name && query.product_name !== '') {
    q.product_name = query.product_name
  }
  return request({
    url: '/api/users/' + user_id + '/sold-products',
    method: 'get',
    params: q
  })
}

export function getStockoutProducts(query) {
  const q = {
    page: query.page,
    limit: query.limit
  }
  if (query.category && query.category !== '') {
    q.category_id = query.category
  }
  if (query.sub_category && query.sub_category !== '') {
    q.category_id = query.sub_category
  }
  if (query.product_name && query.product_name !== '') {
    q.product_name = query.product_name
  }
  return request({
    url: '/api/product/stockout',
    method: 'get',
    params: q
  })
}

export function fetchCateList() {
  return request({
    url: '/api/categories',
    method: 'get'
  })
}

export function uploadFile(data) {
  return request({
    url: '/api/files',
    headers: { 'Content-Type': 'multipart/form-data' },
    method: 'post',
    data
  })
}

export function addProduct(data) {
  return request({
    url: '/api/products',
    method: 'post',
    data
  })
}

export function addCategory(data) {
  return request({
    url: '/api/categories',
    method: 'post',
    data
  })
}

export function updateCategory(id,data) {
  return request({
    url: '/api/categories/' + id,
    method: 'patch',
    data
  })
}


export function deleteProduct(pid) {
  return request({
    url: '/api/products/' + pid,
    method: 'delete'
  })
}

export function updateProduct(pid, data) {
  return request({
    url: '/api/products/' + pid,
    method: 'put',
    data
  })
}

export function addProductItem(pid, data) {
  return request({
    url: '/api/products/' + pid + '/items',
    method: 'post',
    data
  })
}

export function updateProductItem(pid, iid, data) {
  return request({
    url: '/api/products/' + pid + '/items/' + iid,
    method: 'patch',
    data
  })
}

export function deleteProductItem(pid, iid) {
  return request({
    url: '/api/products/' + pid + '/items/' + iid,
    method: 'delete'
  })
}

export function updateProductParam(pid,data){
  return request({
    url: '/api/products/' + pid,
    method: 'patch',
    data
  })
}
